define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {
    function GetQueryString(name) {
        var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)","i");
        var r = window.location.search.substr(1).match(reg);
        if (r!=null) return (r[2]); return null;
    }
    var get_data = GetQueryString("status");
    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'product_lists/products/index' + location.search,
                    add_url: 'product_lists/products/add',
                    edit_url: 'product_lists/products/edit',
                    del_url: 'product_lists/products/del',
                    multi_url: 'product_lists/products/multi',
                    table: 'products',
                }
            });

            var table = $("#table");

            // 初始化表格
            if(get_data == 'radio'){
                table.bootstrapTable({
                    url: $.fn.bootstrapTable.defaults.extend.index_url,
                    pk: 'id',
                    sortName: 'id',
                    columns: [
                        [
                            {checkbox: true,radio:true},
                            {field: 'id', title: __('Id')},
                            {field: 'categorys_id', title: __('Categorys_id')},
                            {field: 'bn', title: __('Bn')},
                            {field: 'name', title: __('Name')},
                            {field: 'spec', title: __('Spec')},
                            {field: 'categorys.name', title: __('Categorys.name')},
                            {field: 'unit', title: __('Unit')},
                            {field: 'weight', title: __('Weight')},
                            {field: 'color', title: __('Color')},
                            {field: 'access', title: __('Access'), searchList: {"self":__('Self'),"purchase":__('Purchase'),"outsourcing":__('Outsourcing'),"virtual":__('Virtual'),"custorm":__('Custorm')}, formatter: Table.api.formatter.normal},
                            {field: 'status', title: __('Status'), searchList: {"0":__('Status 0'),"1":__('Status 1')}, formatter: Table.api.formatter.status},
                            {field: 'working', title: __('Working')},
                            //{field: 'testfiles', title: __('Testfiles')},
                            //{field: 'picbn', title: __('Picbn')},
                            //{field: 'Pic', title: __('Pic')},
                            //{field: 'testpic', title: __('Testpic')},
                            {field: 'custorm_id', title: __('Custorm_id')},
                            {field: 'comment', title: __('Comment')},
                            {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                            {field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                            {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                        ]
                    ]
                });
            }else{
                table.bootstrapTable({
                    url: $.fn.bootstrapTable.defaults.extend.index_url,
                    pk: 'id',
                    sortName: 'id',
                    columns: [
                        [
                            {checkbox: true},
                            {field: 'id', title: __('Id')},
                            {field: 'categorys_id', title: __('Categorys_id')},
                            {field: 'bn', title: __('Bn')},
                            {field: 'name', title: __('Name')},
                            {field: 'spec', title: __('Spec')},
                            {field: 'categorys.name', title: __('Categorys.name')},
                            {field: 'unit', title: __('Unit')},
                            {field: 'weight', title: __('Weight')},
                            {field: 'color', title: __('Color')},
                            {field: 'access', title: __('Access'), searchList: {"self":__('Self'),"purchase":__('Purchase'),"outsourcing":__('Outsourcing'),"virtual":__('Virtual'),"custorm":__('Custorm')}, formatter: Table.api.formatter.normal},
                            {field: 'status', title: __('Status'), searchList: {"0":__('Status 0'),"1":__('Status 1')}, formatter: Table.api.formatter.status},
                            {field: 'working', title: __('Working')},
                            //{field: 'testfiles', title: __('Testfiles')},
                            //{field: 'picbn', title: __('Picbn')},
                            //{field: 'Pic', title: __('Pic')},
                            //{field: 'testpic', title: __('Testpic')},
                            {field: 'custorm_id', title: __('Custorm_id')},
                            {field: 'comment', title: __('Comment')},
                            {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                            {field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                            {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                        ]
                    ],
                    searchFormVisible: true
                });
            }


            // 为表格绑定事件
            Table.api.bindevent(table);
            //点击按钮获取选中的table中的数据
            $("#submit").on("click",function() {
                var temp = table.bootstrapTable('getSelections');
                Fast.api.ajax({
                    url: 'product_lists/products/checkdata',
                    data: {
                        temp: temp
                    }
                }, function (data, ret) {
                    //成功的回调
                    Fast.api.close(data);//这里是重点
                }, function (data, ret) {
                });
            });
            $("#bomlist").on("click",function() {
                var temp = table.bootstrapTable('getSelections');
                Fast.api.ajax({
                    url: 'product_lists/products/bomlist',
                    data: {
                        temp: temp
                    }
                }, function (data, ret) {
                    //成功的回调
                    return false;
                }, function (data, ret) {
                    return false;
                });
            });
        },
        recyclebin: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    'dragsort_url': ''
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: 'product_lists/products/recyclebin' + location.search,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'name', title: __('Name'), align: 'left'},
                        {
                            field: 'deletetime',
                            title: __('Deletetime'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: 'operate',
                            width: '130px',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'Restore',
                                    text: __('Restore'),
                                    classname: 'btn btn-xs btn-info btn-ajax btn-restoreit',
                                    icon: 'fa fa-rotate-left',
                                    url: 'product_lists/products/restore',
                                    refresh: true
                                },
                                {
                                    name: 'Destroy',
                                    text: __('Destroy'),
                                    classname: 'btn btn-xs btn-danger btn-ajax btn-destroyit',
                                    icon: 'fa fa-times',
                                    url: 'product_lists/products/destroy',
                                    refresh: true
                                }
                            ],
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        bomlist: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});